import store from '@/store/store.js'
import config from '@/common/config.js'
import util from '@/common/util.js'

/**
 * @typedef {Object} UploadFileOptions
 * @property {string} tempFilePath
 */
/**
 * 上传单个文件
 * @param {UploadFileOptions} options
 */
export const uploadFile = (options) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: config.baseURL + '/common/upload',
      header: { Authorization: 'Bearer ' + util.getToken() },
      filePath: options.tempFilePath,
      name: 'file',
      success: (uploadFileRes) => {
        if (uploadFileRes.statusCode >= 200 && uploadFileRes.statusCode < 300) {
          const result = JSON.parse(uploadFileRes.data)
          if (result.code === 200) {
            resolve(result)
          } else {
            console.log('上传失败，code 不是 200', result)
            if (!options.cHideError) {
              uni.showToast({ title: '上传失败 ' + result.msg, icon: 'none' })
            }
            if (result.code === 401) {
              console.log('登录过期')
              store.dispatch('LogOut')
              uni.redirectTo({ url: '/pages/login/role' })
            }
            reject('上传失败：' + result.msg)
          }
        } else {
          console.log('上传失败，Status Code 大于 200', uploadFileRes)
          if (!options.cHideError) {
            uni.showToast({ title: '上传失败', icon: 'none' })
          }
          reject('上传失败')
        }
      },
      fail: (error) => {
        console.log('上传失败，uploadFile 接口调用失败', error)
        if (!options.cHideError) {
          uni.showToast({ title: '图片上传失败 ' + error.errMsg, icon: 'none' })
        }
        reject(error)
      },
    })
  })
}
// 修改头像
export const changeAvatar = (options) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: config.baseURL + '/system/user/profile/avatar',
      header: { Authorization: 'Bearer ' + util.getToken() },
      filePath: options.tempFilePath,
      name: 'avatarfile',
      success: (uploadFileRes) => {
        if (uploadFileRes.statusCode >= 200 && uploadFileRes.statusCode < 300) {
          const result = JSON.parse(uploadFileRes.data)
          if (result.code === 200) {
            resolve(result)
          } else {
            console.log('上传失败，code 不是 200', result)
            if (!options.cHideError) {
              uni.showToast({ title: '上传失败 ' + result.msg, icon: 'none' })
            }
            if (result.code === 401) {
              console.log('登录过期')
              store.dispatch('LogOut')
              uni.redirectTo({ url: '/pages/login/role' })
            }
            reject('上传失败：' + result.msg)
          }
        } else {
          console.log('上传失败，Status Code 大于 200', uploadFileRes)
          if (!options.cHideError) {
            uni.showToast({ title: '上传失败', icon: 'none' })
          }
          reject('上传失败')
        }
      },
      fail: (error) => {
        console.log('上传失败，uploadFile 接口调用失败', error)
        if (!options.cHideError) {
          uni.showToast({ title: '图片上传失败 ' + error.errMsg, icon: 'none' })
        }
        reject(error)
      },
    })
  })
}

/**
 * 上传多个文件
 * @param {UploadFileOptions[]} fileList
 */
export const uploadFileList = (fileList) => {
  const arr = fileList.map((item) => uploadFile(item))
  return Promise.all(arr)
}
